package struts.util;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;


public class StrutsConfig {
	public static Map<String, XMLBean> ParsingXML(String path) throws JDOMException, IOException {
		SAXBuilder builder=new SAXBuilder();
		Document document=builder.build(new File(path));
		Element root= document.getRootElement();
		Map<String,XMLBean> xmlMap=new HashMap<String,XMLBean>();
		Element actionRoot=root.getChild("action-mapping");
		List<Element> action=actionRoot.getChildren();
		for (Element element : action) {
			XMLBean actinBean=new XMLBean();
			String name=element.getAttributeValue("name");
			actinBean.setBeanName(name);
			Element actionForm=root.getChild("formBeans");
			List<Element> form=actionForm.getChildren();
			for (Element e : form) {
				if(name.equals(e.getAttributeValue("name"))) {
					String formClass=e.getAttributeValue("class");
					actinBean.setFormClass(formClass);
				}
			}
			String classPath=element.getAttributeValue("path");
			actinBean.setPath(classPath);
			String type=element.getAttributeValue("type");
			actinBean.setActionType(type);
			List<Element> forword=element.getChildren();
			Map<String,String> map=new HashMap<String,String>();
			for (Element element2 : forword) {
				String forName=element2.getAttributeValue("success");
				String forVal=element2.getAttributeValue("value");
				map.put(forName, forVal);
			}
			actinBean.setForwardMap(map);
			xmlMap.put(classPath, actinBean);
		}
		return xmlMap;
	}
}
